package com.ucar.training.controller;

import com.ucar.training.common.ResponseEnum;
import com.ucar.training.common.Result;
import com.ucar.training.service.IUserService;
import com.ucar.training.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author XiongMM
 * @date 2019/7/29 15:47
 * @description 删除一个用户
 */
public class DeleteUserServlet extends HttpServlet {

    private IUserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("userName");
        Result result = userService.delUser(userName);
        String message;
        if(result.getCode().name().equals(ResponseEnum.SUCCESS.name())){
             message = String.format(
                    "恭喜：删除用户：【%s】成功！本页将在3秒后跳到首页！！<meta http-equiv='refresh' content='3;url=%s'",
                    userName,
                    request.getContextPath()+"/final/userinfo");
        }else{
            message = String.format(
                    "糟糕：删除用户：【%s】失败 ！！！本页将在3秒后跳到首页！！<meta http-equiv='refresh' content='3;url=%s'",
                    userName,
                    request.getContextPath()+"/final/userinfo");
        }
        request.setAttribute("message",message);
        request.getRequestDispatcher("/message.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
